@auth.requires_login()
def index():
    app_settings = 'app settings'
    record = db(db.settings).select().first()
    users = db().select(db.users.ALL, orderby=~db.users.join_time)
    user_number = len(users)
    last_join_time = users.first().join_time
    form = SQLFORM(db.settings, record = record,
            labels = {
                'fb_permissions' : '%s/permissions' % app_settings,
                'fb_app_id'      : '%s/app id' % app_settings,
                'fb_app_secret'  : '%s/app secret' % app_settings,
                'fb_canvas_page' : '%s/cavnas page' % app_settings,
                'fb_canvas_url' : '%s/canvas URL' % app_settings,
                'cpa' : 'CPA/CPA widget code',
                'landing_enable' : 'Landing Page',
                'landing_content': 'Landing Page Design',
                'first_step' : 'First Step(what gets them to invite)',
                'app_content': 'Actual Content',
                'invites_enable' : 'App Request Invites',
                'auto_invites_enable' : 'Auto Loading of Request Dialog',
                'force_invites_enable' : 'Force Invites',
                'force_count_enable' : 'Force Counting',
                'invite_title' : 'Invite Title',
                'invite_message' : 'Invite Message',
                'force_count_number' : 'Force Count #',
                'force_count_before' : 'Force Message Before \#',
                'force_count_after' : 'Force Message After \\#',
                },
            col3 = {
                'fb_permissions' : I('ex. offline_access,publish_stream'),
                'fb_canvas_page' : I('requires trailing slash /'),
                'fb_canvas_url' : I('requires trailing slash /'),
                'cpa' : SPAN('Breaks FB TOS', _style='color:red'),
                'landing_enable' : I('Check this to enable langing page'),
                'landing_content' : I('You may use HTML and some JS here'),
                'first_step' : I('You may use HTML and some JS here'),
                'app_content' : I('You may use HTML and some JS here'),
                'invites_enable' : I('Check this to enable app request invites'),
                'auto_invites_enable' : I('Check this to enable auto loading of request dialog'),
                'force_invites_enable' : I('Check this to enable Force Invites', SPAN('(Breaks FB TOS)', _style='color:red')),
                'force_count_enable' : I('Check this to enable Force Countring', SPAN('(Breaks FB TOS)', _style='color:red')),
                'invite_title' : I('Max 55 characters'),
                'invite_message' : I('Max 255 characters'),
                },
                )
    if form.accepts(request.vars, session):
        response.flash = 'Settings saved!'
    elif form.errors:
        response.flash = 'Please check the error'
    else:
        response.flash = 'Please fill out the fields'
    return dict(form = form, user_number = user_number, last_join_time = last_join_time)

def error():
    return dict(message = T("You need to log in first!"))
